﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UIT.Controls.Containers;
using System.Xml;
using UIT.Controls;
using UIT.Controls.Input;
using UIT.Web.Controls;
using UIT.Web.Renderers;
using UIT.Controls.Data;
using UIT.Web.Controls.Html;
using UIT.Controls.Template;
using UIT.Web.Controls.Template;
using System.Web.Compilation;
using UIT.Web.Controls.Data;
using UIT.Animations;
using UIT.Web.Renderers.Animations;

namespace UIT.Web
{
    public class WebRendererManager
    {
        private static Type GetType(string typeName)
        {
            return BuildManager.GetType(typeName, true);
        }

        public WebRendererManager(XmlWriter writer)
        {
            this.writer = writer;
        }

        RendererManager manager;
        private XmlWriter writer;
        internal RendererManager Manager
        {
            get
            {
                if (manager == null)
                {
                    manager = new RendererManager(Initialize);
                    manager.Control.RegisterRenderer<ButtonRenderer, Button>();
                    manager.Control.RegisterRenderer<PageRenderer, UIT.Controls.Containers.Page>();
                    manager.Control.RegisterRenderer<TextBoxRenderer, TextBox>();
                    //manager.RegisterControlRenderer<FormRenderer, Form>();
                    manager.Control.RegisterRenderer<RepeaterRenderer, Repeater>();
                    manager.Control.RegisterRenderer<LineBreakRenderer, LineBreak>();
                    manager.Control.RegisterRenderer<HtmlControlRenderer, HtmlControl>();
                    manager.Control.RegisterRenderer<LinkRenderer, Link>();
                    manager.Control.RegisterRenderer<DataGridRenderer, DataGrid>();
                    manager.Control.RegisterRenderer<DataGridRenderer, DataGrid.Row>();
                    //manager.RegisterTemplateRenderer<ClientTemplateRenderer, DataTemplate>();
                    manager.Control.RegisterRenderer<WrapPanelRenderer, WrapPanel>();
                    manager.Control.RegisterRenderer<StackPanelRenderer, StackPanel>();
                    manager.Control.RegisterRenderer<ItemsControlRenderer, ItemsControl>();
                    manager.Control.RegisterRenderer<ItemsControlRenderer, SelectableItemsControl>();
                    manager.Control.RegisterRenderer<BorderRenderer, Border>();
                    manager.Control.RegisterRenderer<DataTemplateInstanceRenderer, DataTemplateInstance>();
                    manager.Control.RegisterRenderer<TextBlockRenderer, TextBlock>();
                    manager.Animation.RegisterRenderer<UnitAnimationRenderer, UnitAnimation>();
                }
                return manager;
            }
        }

        private void Initialize(IRenderer renderer)
        {
            IRenderer webRenderer = renderer as IRenderer;
            if (webRenderer != null)
                webRenderer.Initialize(Manager, writer);
        }

        public static void Render(UIT.Controls.Containers.Page p, XmlWriter writer)
        {
            var renderer = new WebRendererManager(writer);
            ((Context)UIT.Context.Current).RendererManager = renderer.Manager;
            renderer.Manager.Control.GetRenderer<UIT.Controls.Containers.Page>().Render(p);
        }

        private static bool initialized;

        internal static void EnsureInitialized()
        {
            if (initialized)
                return;
            initialized = true;
            RendererManager.TypeNeeded += GetType;
            RendererManager.ReferencesNeeded += new Action<List<System.Reflection.Assembly>>(RendererManager_ReferencesNeeded);
            RendererManager.Mode = RenderingMode.Web;
        }

        static void RendererManager_ReferencesNeeded(List<System.Reflection.Assembly> obj)
        {
            obj.AddRange(PageHandler.references);
        }
    }
}
